<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button @click="onAddChannel">添加分类</el-button>
    </template>
    <el-table
      :data="channelList"
      stripe
      style="width: 100%"
      v-loading="isLoading"
    >
      <!-- 下面这一行可以增加序号 -->
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="cate_name" label="文章分类名称" />
      <el-table-column prop="cate_alias" label="文章分类别名" />
      <el-table-column prop="" label="操作" width="100">
        <!-- 解构对象 ，拿出row,$index值 -->
        <!-- row 类似 v-for 拿到的item，对应的行的数据 和索引 index -->
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="onEditChannel(row)"
          />
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="onDelChannel(row)"
          />
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <channelEdit ref="dialog" @success="onSuccess"></channelEdit>
  </page-container>
</template>
<script lang="ts" setup name="">
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { artGetChannelsService, artDelChannelService } from '@/api/article'
import channelEdit from '@/components/channelEdit.vue'

const channelList = ref([])
const isLoading = ref(false)
const dialog = ref()

const artGetChannels = async () => {
  try {
    isLoading.value = true
    const artGetChannelsRes = await artGetChannelsService()
    // console.log(artGetChannelsRes)
    channelList.value = artGetChannelsRes.data.data
    isLoading.value = false
  } catch (error) {
    console.log(error)
  }
}
artGetChannels()

const onEditChannel = (row: any) => {
  // console.log(row, index)
  // console.log(dialog.value.open)
  dialog.value.open({ row })
}
const onDelChannel = async (row: any) => {
  await ElMessageBox.confirm('是否删除该分类', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
  // console.log(row, index)
  const res = await artDelChannelService(row.id)
  console.log(res)
  ElMessage.success('删除成功')
  onSuccess(0)
}
const onAddChannel = () => {
  // console.log(row, index)
  dialog.value.open({})
}
const onSuccess = (isEdit: number) => {
  artGetChannels()
  console.log(isEdit)
}
</script>
<style scoped lang="scss"></style>
